<template>
  <el-form-item :class="getClz(['form_checkbox'])" :prop="opt.prop" v-if="!opt.hide" >
    <div slot="label" style="display: inline-block">{{opt.text}}<el-popover v-if="Boolean(opt.textLabel)" placement="top" trigger="hover"><div>{{opt.textLabel}}</div><i slot="reference" class="el-icon-warning" /></el-popover></div>
    <el-checkbox-group
      v-model="value"
      v-bind="opt.props"
      @change="handleChange">
      <el-checkbox
        v-for="option in opt.options"
        :key="option[opt.optionsValue]"
        :label="option[opt.optionsValue]"
        :disabled="option.disabled"
      >
        {{option[opt.optionsLabel]+''}}
      </el-checkbox>
    </el-checkbox-group>
  </el-form-item>
</template>

<script>
  import extend from '../extend';
  let Log = ()=>{};

  export default {
    name: "FormCheckboxStr",
    mixins:[extend],
    dicts:[],
    props: {
      column: {
        type:[Object,Array],
        default:{}
      },
      data:{
        type:[Object,Array],
        default:{}
      }
    },
    data(){
      return {
        value:[],
        opt:{
          name:'',
          text:'',
          prop:'',
          props:{
            disabled:false
          },
          hide:false,
          placeholder:'',
          hasNull:false,
          options:[],
          optionsLabel:'label',
          optionsValue:'value'
        }
      }
    },
    created(){
      this.initOptions();
      this.onChange = this.column.onChange||Log;
      //
      this.$watch(`data.${this.column.name}`,(val)=>{
        this.value = val.split(',')
      })
    },
    methods:{
      setValue(val){
        this.data[this.opt.name] = val;
      },
      setValueMap(val){
        this.onChange(this.data,this.column);
      },
      handleChange(){
        let str = this.value.join(',');
        this.setValue(str)
        this.setValueMap(str)
      }
    }
  }
</script>

<style scoped>
  .form_checkbox{text-align: left;}
</style>
